package threadUtils.demo01;

import threadUtils.Goods;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * 门阀计数器的使用
 */
public class CountDownLatchExample {

    public static void main(String[] args) throws InterruptedException {

        List<Goods> list = Goods.getGoodsList();

        final CountDownLatch downLatch = new CountDownLatch(list.size());
        list.forEach(g -> {
            new Thread(() -> {
                try {
                    // 模拟业务逻辑
                    TimeUnit.MILLISECONDS.sleep(20);
                    g.setThreadContent(Thread.currentThread().getName());
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    downLatch.countDown();
                }

            }).start();
        });
        downLatch.await();
        System.out.println("all of goods has finished");
        list.forEach(System.out::println);
    }

}

